using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using CodeParent.Core.Cfg;

namespace CodeParent.Win
{
    class UserOption
    {
        private string _interfacePath;
        private string _modulePath;
        private XmlDocument _document = new XmlDocument();

        public UserOption()
        {
            _document.Load("Configuration.xml");

            XmlNode node = SelectNode(_document).SelectSingleNode("//InterfaceFolder");
            _interfacePath = node.InnerText;

            node = SelectNode(_document).SelectSingleNode("//ClassFolder");
            _modulePath = node.InnerText;
        }

        public string InterfacePath
        {
            get { return _interfacePath; }
            set { _interfacePath = value; }
        }

        public string ModulePath
        {
            get { return _modulePath; }
            set { _modulePath = value; }
        }

        public void Save()
        {
            XmlNode node = SelectNode(_document).SelectSingleNode("//InterfaceFolder");
            node.InnerText = _interfacePath;

            node = SelectNode(_document).SelectSingleNode("//ClassFolder");
            node.InnerText = _modulePath;

            _document.Save("Configuration.xml");
        }

        private XmlNode SelectNode(XmlDocument document)
        {
            string xPath = String.Format("//EnviromentSettings/EnviromentSetting");
            XmlNodeList nodeList = _document.SelectNodes(xPath);
            foreach (XmlNode nextNode in nodeList)
            {
                if (nextNode.Attributes["ID"].Value.Equals("default"))
                    return nextNode;
            }

            return null;
        }
    }
}
